package hotel.service.impl;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import hotel.entity.Category;
import hotel.entity.Room;
import hotel.service.IRoomService;

@Component
public class RoomServiceImpl<T> extends ServiceImpl<T> implements IRoomService<T> {

	@SuppressWarnings("unchecked")
	@Override
	public Map<Integer,Room> findAvailableRoomByCategory(int categoryid) {
		Map<Integer,Room> map=new HashMap<Integer,Room>();
		List<Room> list=(List<Room>)dao.list("from Room r where r.category.categoryid = "+categoryid+" AND r.state = '空'");
		for(Room r:list) {
			map.put(r.getRoomid(),r);
		}
		return map;
	}

	@Override
	public List<T> findRoomByCategory(int categoryid) {
		return dao.list("from Room r where r.category.categoryid = "+categoryid);
	}

	@Override
	public int getAvailableCount() {
		return Integer.parseInt(dao.uniqueResult("select count(*) from Room r where r.state = '空'"));
	}

	@Override
	public boolean changeRoom(int itemid, int newRoomid, int oldRoomid) {
		try {
			dao.executeUpdate("update Room r set r.state ='空' where r.roomid ="+oldRoomid);
			dao.executeUpdate("update Room r set r.state ='已入住' where r.roomid ="+newRoomid);
			dao.executeUpdate("update SalesLineItem item set item.room.roomid = "+newRoomid+"where item.itemid = "+itemid);
			return true;
		}catch(Exception ex) {
			ex.printStackTrace();
		}
		return false;
	}

	@Override
	public Map<String, Double> getRoomStatistics() {
		Map<String,Double> map=new HashMap<String,Double>();
		int total=dao.getTotalCount("Room");
		double temp=0;
		List<Object[]> list=dao.sqlQuery("select categoryName,COUNT(*) AS amount from room,category WHERE room.category_categoryid=category.categoryid GROUP BY room.category_categoryid");
		for(Object[] obj:list) {
			temp=((BigInteger)obj[1]).intValue()/(total*1.0);
			map.put((String)obj[0], Double.parseDouble(String.format("%.2f", temp)));
		}
		return map;
	}

	@Override
	public int getAvailableCountByCategory(int categoryid) {
		//空房间数量
		String str1=dao.uniqueResult("select count(*) from Room r where r.state = '空' AND r.category.categoryid = "+categoryid);
		if(str1==null)//返回空说明没有空房间，可用房间数量返回0
			return 0;
		int count1= Integer.parseInt(str1);
		if(count1==0)
			return 0;
		//预定数量
		String str2=dao.uniqueResult("select sum(amount) from Reserve r where r.state = '已预定' AND r.category.categoryid = "+categoryid);
		if(str2==null)//返回空，说明没有预定，则空房数量即为可用房间数量
			return count1;
		int count2=Integer.parseInt(str2);
		return count1-count2;
	}

	@Override
	public int getAvailableCountByCategory(Category category) {
		if(category==null)
			return -1;
		return this.getAvailableCountByCategory(category.getCategoryid());
	}

	@Override
	public boolean updateRoomState(int roomid) {
		return dao.executeUpdate("update Room r set r.state = '空' where r.roomid = "+roomid);
	}

}
